package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppProductService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppProductCreateValidate;
import com.mdd.admin.validate.yylq.TAppProductSearchValidate;
import com.mdd.admin.validate.yylq.TAppProductUpdateValidate;
import com.mdd.admin.vo.yylq.TAppProductDetailVo;
import com.mdd.admin.vo.yylq.TAppProductListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppProduct;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppProductMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * app产品实现类
 * @author LikeAdmin
 */
@Service
public class TAppProductServiceImpl implements ITAppProductService {
        
    @Resource
    TAppProductMapper tAppProductMapper;

    /**
     * app产品列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppProductListedVo>
     */
    @Override
    public PageResult<TAppProductListedVo> list(PageValidate pageValidate, TAppProductSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(Arrays.asList("sort", "id"));

        tAppProductMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:pic:str",
            "like:title:str",
            "=:subtitle:str",
            "=:sort:int",
        });

        IPage<TAppProduct> iPage = tAppProductMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppProductListedVo> list = new LinkedList<>();
        for(TAppProduct item : iPage.getRecords()) {
            TAppProductListedVo vo = new TAppProductListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.formatDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.formatDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * app产品详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppProduct
     */
    @Override
    public TAppProductDetailVo detail(Integer id) {
        TAppProduct model = tAppProductMapper.selectOne(
                new QueryWrapper<TAppProduct>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppProductDetailVo vo = new TAppProductDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * app产品新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppProductCreateValidate createValidate) {
        TAppProduct model = new TAppProduct();
        model.setPic(createValidate.getPic());
        model.setTitle(createValidate.getTitle());
        model.setSubtitle(createValidate.getSubtitle());
        model.setSort(createValidate.getSort());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        tAppProductMapper.insert(model);
    }

    /**
     * app产品编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppProductUpdateValidate updateValidate) {
        TAppProduct model = tAppProductMapper.selectOne(
                new QueryWrapper<TAppProduct>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setPic(updateValidate.getPic());
        model.setTitle(updateValidate.getTitle());
        model.setSubtitle(updateValidate.getSubtitle());
        model.setSort(updateValidate.getSort());
        model.setUpdateTime(new Date());
        tAppProductMapper.updateById(model);
    }

    /**
     * app产品删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppProduct model = tAppProductMapper.selectOne(
                new QueryWrapper<TAppProduct>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppProductMapper.delete(new QueryWrapper<TAppProduct>().eq("id", id));
    }

}
